﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;

namespace Ession
{
    /// <summary>
    /// 实现自动利用一系列的数据，采取固定的动作。
    /// </summary>
    public class EssionRobot
    {
        public EssionRobot(int threadCount, List<string> data, Uri uri)
        {
            this.ThreadCount = threadCount;
            this.Data = data;
            threads = new DownloaderThread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                threads[i] = new DownloaderThread();
            }
            this.Uri = uri;
            if (threadCount <= 10)
            {
                ServicePointManager.DefaultConnectionLimit = threadCount;
            }
            else
            {
                ServicePointManager.DefaultConnectionLimit = 10;
            }

        }
        public string  OKString="not set";
        public List<string> pwds;
        private List<string> data;
        private int interval = 1100;

        private int oKLength = 0;
        private int threadCount = 1;
        private DownloaderThread[] threads;
        private Uri uri;
        /// <summary>
        /// 所有数据
        /// </summary>
        public List<string> Data
        {
            get { return data; }
            set { data = value; }
        }
        /// <summary>
        /// 每个线程发送的间隔
        /// </summary>
        public int Interval
        {
            get { return interval; }
            set { interval = value; }
        }

        /// <summary>
        /// 正确的字节大小
        /// </summary>
        public int OkLength
        {
            get { return oKLength; }
            set { oKLength = value; }
        }
        /// <summary>
        /// 最大的线程数
        /// </summary>
        public int ThreadCount
        {
            get { return threadCount; }
            set { threadCount = value; }
        }

        public Uri Uri
        {
            get { return uri; }
            set { uri = value; }
        }

        public void StartGET()
        {

        }
        public void StartPOST()
        {
            try
            {
                DownloaderThread dt = null;
                foreach (var d in Data)
                {
                    dt = GetAvaibleThread();
                    while (dt == null)
                    {
                        Thread.Sleep(1000);
                        dt = GetAvaibleThread();
                    }
                    dt.uri = uri;
                    dt.Data = d;
                    dt.OKString = this.OKString;
                    dt.foundPwd += new EventHandler(dt_foundPwd);
                    dt.Start();
                    Thread.Sleep(3000);
                }
            }
            catch (Exception e)
            {

            }

        }

        void dt_foundPwd(object sender, EventArgs e)
        {
            if (pwds == null)
            {
                pwds = new List<string>();
            }
            DownloaderThread dt = sender as DownloaderThread;
            if (dt != null)
            {
                if (!pwds.Contains(dt.foundPwdStr))
                {
                    pwds.Add(dt.foundPwdStr);
                }
            }
        }

        private DownloaderThread GetAvaibleThread()
        {
            foreach (var downloaderThread in threads)
            {
                //if (downloaderThread.downloading == false)
                //{
                //    return downloaderThread;
                //}
                if (downloaderThread.thread==null)
                {
                    return downloaderThread;
                }
                if (downloaderThread.thread.ThreadState!=ThreadState.Running)
                {
                    return downloaderThread;
                }
            }
            return null;
        }
        //TODO: 间隔
    }


}
